<?php
class fEditNews extends aFeature 
{

  public function __construct() 
  {
    $this->_acl = new AccessControlList();
    // $this->_acl->setGranted(3); 
    $this->_acl->setGranted(7); 
  }

  public static function getTocEntry()
  {
    return "Modifier une news";
  }

  public static function getRealName() 
  {
    return "Interface de modification des news";
  }

  public static function getSemantic()
  {
    return "Cette fonction permet de modifier les news présente dans le système";
  }

  protected function run() 
  {
    $id = PropRetriever::getProp($_GET,"id");
    if (PropRetriever::getProp($_POST,"id"))
      $id = PropRetriever::getProp($_POST,"id");
    $action = PropRetriever::getProp($_POST,"action");
    if( $id == "")
      return $this->displayAll();
    else
      if ($action == "")
	return $this->displayEditForm($id);
      else
	return $this->doEdition($id);
  }

  private function displayAll($text="")
  {
    $page = "<h1> Modifier une news ... </h1>";
    $page .= $text;
    $page .= News::showResult(News::lookFor(Login::seasonChoosen()),true);
    return $page;
  }

  private function doEdition($id)
  {
    $aNews = News::read($id);
    $aNews->setTitle(PropRetriever::getProp($_POST,"title"));
    $aNews->setAuthor(PropRetriever::getProp($_POST,"author"));
    $aNews->setContent(PropRetriever::getProp($_POST,"content"));
    $aNews->update();
    $result = <<<EOS
<h2>Modification effectuée avec succès </h2>
<p>votre modification est prise en compte par le système. Elle sera visible sur le site externe lors de la prochaine mise a jour </p>
EOS;
    return $this->displayall($result);
  }

  private function displayEditForm($id)
  {
    $aNews = News::read($id);
    $title = $aNews->getTitle();
    if(PropRetriever::getProp($_POST,"title") != "")
      $title = PropRetriever::getProp($_POST,"title");
    $author = $aNews->getAuthor();
    if(PropRetriever::getProp($_POST,"author") != "")
      $author = PropRetriever::getProp($_POST,"author");
    $content = $aNews->getcontent();
    if(PropRetriever::getProp($_POST,"content") != "")
      $content = PropRetriever::getProp($_POST,"content");
    $page = <<<EOS
<h1> Modification d'une news </h1> <!-- ' -->
<div align="center">
<form method="post" accept-charset="utf-8" name="addMatche" action="?add" >
<input type="hidden" name="action" value="update" />
<input type="hidden" name="id" value="${id}" />
<fieldset>
      <legend>News</legend>
      <br />
      <label> 
        <span>Titre : </span>
        <input type="text" name="title" class="edit" value="${title}" />
        &nbsp; &nbsp;
      </label> <br />
      <label>
        <span>Auteur : </span>
        <input type="text" name="author" class="edit" value="${author}" />
        &nbsp; &nbsp;
      </label>
      <label>
	  <div align="left"> <span> Texte : </span> </div>
        <textarea name="content" class="edit" rows="15">${content}</textarea> 
       </label>
       <br /> <br />
       <input type="submit" value="Modifier" class="edit" />
							     &nbsp; &nbsp;
       <input type="reset" value="Remise a Zéro" class="edit" />
</fieldset>
</form>
</div>
EOS;
    return $page;
  }

}
?>